home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_400
/
416_01
/
grep
/
grepfv10.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-13
|
21KB
|
815 lines
/* GREPFV10 - Microsoft C source
Find regular expression in files and archives
=======================================================
Nigel Salt
25 Lower Station Rd
Crayford
Kent
DA1 3PY
UK
Phone +44 322 553260
Email nao@cix.compulink.co.uk
=======================================================
Can process PAK ZIP ARC ARJ LZH and ZOO files
grepfv expression filepattern[+] tempdir
MSoft C V6
[Options]
Argv 1 regular expression
2 file pattern
3 Path for temporary files
FV.COM must be in path along with any necessary
utilities to unarchive files
+ on the end of file pattern activates recursive directory search
*/
/**********************************************************************
The getfiles function is recursive if search of subdirectories
is specified.
This can cause stack overflow which may be remedied by compiling as
cl -F D000 grepfv.c
This gives the program a very large stack
***********************************************************************/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <dos.h>
#include <string.h>
#include <time.h>
/* Prototypes */
int ifile(char *fname);
int xfile(char *fname);
void dosearch(char *name);
void doarc(char *arcname);
void dofile(char *fname);
void usage(char *error);
char *regstr(char *regexp,char *string);
int regpars(char *r);
void nsfgets(char *buff,int max,FILE *f);
void getfiles(char *p,char *n,int all);
void dosummary(void);
/* Global variables */
/* Structure to hold regular expression segments */
typedef struct
{
char op;
char *cptr;
} REGSEG;
REGSEG regsegs[64];
int numexp;
/* Structure to hold file exclusion / inclusion strings
sptr points to a string
next is NULL or a pointer to the next item in the list */
typedef struct lstruct
{
char *sptr;
struct lstruct *next;
} LTYPE, *LTYPEPTR;
static LTYPE xlist;
static LTYPE ilist;
static char tempath[256];
static char zippath[256];
static char buff[256];
static char regexp[256];
static char version[]="GREPFV10";
static char copyright[]=" (c) Nigel Salt 1994";
static int checksum=1422;
static unsigned long numfound=0;
static unsigned long numarcs=0;
static unsigned long numfiles=0;
static unsigned long numfailed=0;
static unsigned long numexc=0;
static unsigned long numchars=0;
static time_t startime;
static time_t endtime;
static FILE *nsout=NULL;
static char *outfilnam;
static int outfile=0;
void main(int argc,char *argv[])
{
int all=0;
int pattend;
int nextarg=0;
char path[129];
char name[65];
char *fslash;
char *lastslash;
char *s;
int count;
char *arglist[256];
LTYPEPTR xcurptr,icurptr;
FILE *optfile;
int i;
int maxarg=0;
int firstarg=1;
char *tokadd;
/* Set start time in seconds */
time(&startime);
/* Set include list to empty */
icurptr=&ilist;
icurptr->sptr=NULL;
icurptr->next=NULL;
/* Set exclude list to empty */
xcurptr=&xlist;
xcurptr->sptr=NULL;
xcurptr->next=NULL;
/* Check copyright notice */
s=copyright;
count=0;
while (*s)
count+=*s++;
if (count!=checksum)
{
fprintf(stderr,"\nCopyright violation");
exit(1);
}
fprintf(stderr,"%s%s\n",version,copyright);
/* Copy args to list */
if (argv[1][0]=='@')
{
firstarg=2;
optfile=fopen(&argv[1][1],"r");
if (optfile==NULL)
{
sprintf(buff,"Cannot open option file '%s'",&argv[1][1]);
usage(buff);
}
while (!feof(optfile) && maxarg<256)
{
if (fgets(buff,255,optfile)==NULL) continue;
if (feof(optfile)) continue;
tokadd=strtok(buff,"\n\r\t ");
while (tokadd!=NULL && maxarg<256)
{
if ( (arglist[maxarg]=calloc(1,strlen(tokadd)+1))==NULL)
usage("No memory for parameters from file");
strcpy(arglist[maxarg],tokadd);
maxarg++;
tokadd=strtok(NULL,"\n\r\t ");
}
}
fclose(optfile);
}
for (i=firstarg;i<argc;i++,maxarg++)
{
if (maxarg>255)
usage("Too many parameters - max is 255");
arglist[maxarg]=argv[i];
}
if (maxarg<2)
usage("You must specify an expression and filepattern");
/* Print command line for reference where results redirected to file */
while (arglist[nextarg][0]=='-')
{
switch (toupper(arglist[nextarg][1]))
{
case 'O':
nextarg++;
outfilnam=arglist[nextarg];
nsout=fopen(outfilnam,"r");
if (nsout!=NULL)
{
sprintf(buff,"Output file '%s' already exists!\nDelete it or choose a new output file name"
,outfilnam);
usage(buff);
}
nsout=fopen(outfilnam,"w");
if (nsout==NULL)
{
sprintf(buff,"Cannot open output file '%s'",outfilnam);
usage(buff);
}
outfile=1;
nextarg++;
break;
case 'X':
nextarg++;
strupr(arglist[nextarg]);
xcurptr->next=(LTYPEPTR)(malloc(sizeof(LTYPE)));
if (xcurptr->next==NULL)
usage("Cannot allocate memory for exclude option");
xcurptr->sptr=arglist[nextarg];
nextarg++;
xcurptr->next->next=NULL;
xcurptr->next->sptr=NULL;
xcurptr=xcurptr->next;
break;
case 'I':
nextarg++;
strupr(arglist[nextarg]);
icurptr->next=(LTYPEPTR)(malloc(sizeof(LTYPE)));
if (icurptr->next==NULL)
usage("Cannot allocate memory for include option");
icurptr->sptr=arglist[nextarg];
nextarg++;
icurptr->next->next=NULL;
icurptr->next->sptr=NULL;
icurptr=icurptr->next;
break;
default:
sprintf(buff,"Unrecognised option '%s'",arglist[nextarg]);
usage(buff);
}
}
/* Print options for reference */
fprintf(stdout,"GREPFV10 SEARCH PARAMETERS\nOPTIONS:");
if (outfile)
fprintf(nsout,"GREPFV10 SEARCH PARAMETERS\nOPTIONS:");
icurptr=&ilist;
while (icurptr->sptr!=NULL)
{
fprintf(stdout,"\n\t -i %s",icurptr->sptr);
if (outfile)
fprintf(nsout,"\n\t -i %s",icurptr->sptr);
icurptr=icurptr->next;
}
xcurptr=&xlist;
while (xcurptr->sptr!=NULL)
{
fprintf(stdout,"\n\t -x %s",xcurptr->sptr);
if (outfile)
fprintf(nsout,"\n\t -x %s",xcurptr->sptr);
xcurptr=xcurptr->next;
}
if (outfile)
{
fprintf(stdout,"\n\t -o %s",outfilnam);
fprintf(nsout,"\n\t -o %s",outfilnam);
}
/* Nextarg should now be set to the regexp parameter */
if (nextarg+1>maxarg)
usage("You must specify an expression and filepattern");
/* Get the regular expression */
fprintf(stdout,"\nEXPRESSION:\t%s",arglist[nextarg]);
if (outfile)
fprintf(nsout,"\nEXPRESSION:\t%s",arglist[nextarg]);
strcpy(regexp,arglist[nextarg]);
strupr(regexp);
numexp=regpars(regexp);
nextarg++;
/* Process the file pattern */
fprintf(stdout,"\nFILE PATTERN:\t%s",arglist[nextarg]);
if (outfile)
fprintf(nsout,"\nFILE PATTERN:\t%s",arglist[nextarg]);
strcpy(path,strupr(arglist[nextarg]));
nextarg++;
pattend=strlen(path)-1;
if (path[pattend]=='+')
{
all=1; /* + at end so recurse subdirectories */
path[pattend]='\0';
pattend--;
}
/* Change forward slashes to backslashes */
fslash=strchr(path,'/');
while (fslash!=NULL)
{
*fslash='\\';
fslash=strchr(path,'/');
}
/* Split name and path */
if (path[pattend]=='\\')
strcpy(name,"*.*");
else
{
lastslash=strrchr(path,'\\');
if (lastslash==NULL)
{
strcpy(name,path);
path[0]='\0';
}
else
{
strcpy(name,lastslash+1);
*(lastslash+1)='\0';
}
}
/* Process temporary file path if any */
if (maxarg>nextarg)
{
fprintf(stdout,"\nTEMP PATH:\t%s",arglist[nextarg]);